package cn.javabb.web.controller.pay;

import cn.javabb.common.enums.ResponseEnum;
import cn.javabb.common.model.ResponseModel;
import cn.javabb.core.base.BaseController;
import cn.javabb.core.constant.PayConstant;
import cn.javabb.core.entity.pay.OrderDO;
import cn.javabb.core.entity.sys.UserDO;
import cn.javabb.core.query.OrderQuery;
import cn.javabb.core.service.pay.OrderService;
import cn.javabb.log.action.OrderAction;
import cn.javabb.log.annotation.Log;
import com.github.pagehelper.Page;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author javabb
 * @Date 2019/8/15 0015 下午 5:05
 * @Desc
 */
@RequestMapping("/order")
@Controller
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/myOrder")
    public String toOrderPage(){
        return "web/view/pay/myOrder";
    }
    @ResponseBody
    @GetMapping("/list")
    public Object list(OrderQuery orderQuery){
        UserDO loginUser = getLoginUser();
        if (null == loginUser) {
            return ResponseModel.NoLogin();
        }
        orderQuery.setUserId(loginUser.getId());
        Page p = orderService.queryPageBy(orderQuery);
        return ResponseModel.table(p);
    }

    /**
     * 判断创建订单状态
     * @param goodsId
     * @param payType
     * @return
     */
    @ResponseBody
    @PostMapping("/checkOrder")
    public Object checkPay(Integer goodsId,String payType){
        UserDO loginUser = getLoginUser();
        // 判断用户登录
        if(loginUser==null){
            return ResponseModel.NoLogin();
        }
        // 判断用户是否存在未支付的订单
        OrderDO noPayOrder = orderService.getNoPayOrder(loginUser.getId(),goodsId);
        if(noPayOrder != null){
            ResponseModel res = ResponseModel.Failure(ResponseEnum.EXIST_NOPAY_ORDER.getMessage(), ResponseEnum.EXIST_NOPAY_ORDER.getCode());
            res.setData(noPayOrder.getOrderId());
            return res;
        }
        // 判断是不是已经购买过了
        if(orderService.existOrder(goodsId,loginUser.getId())){
            return ResponseModel.Failure("您已经购买了此产品，无需重复购买");
        }

        return ResponseModel.Success();
    }
    @RequiresAuthentication
    @ResponseBody
    @PostMapping("/createOrder")
    @Log(key= OrderAction.ORDER_CREATE,action = OrderAction.class)
    public Object createOrder(Integer goodsId,String payType){
        UserDO loginUser = getLoginUser();
        // 创建订单
        OrderDO order = orderService.createOrder(payType, goodsId,loginUser.getId());
        if(null == order){
            return ResponseModel.Failure("创建订单失败，请联系管理员");
        }
        return ResponseModel.Success(order.getOrderId(),200);
    }
    @GetMapping("/pay")
    public String toPay(String orderId, Model model){
        if(getLoginUser()==null){
            return "/error/401";
        }
        try {
            OrderDO order = orderService.getOrderById(orderId);
            //PayModel pm = PayUtil.createOrder(0.32,"alipay");
            model.addAttribute("order", order);
            if("alipay".equals(order.getPayType())){
                return "web/view/pay/pay-alipay";
            }
            return "web/view/pay/pay";
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return "/error/500";
    }
    /**
     * 查询订单
     * @param orderId
     * @return
     */
    @ResponseBody
    @GetMapping("/queryOrderState")
    public Object queryOrder(String orderId){
        OrderDO order = orderService.getOrderState(orderId);
        if(order!=null){
            return ResponseModel.Success(order.getState());
        }
        return ResponseModel.Failure();
    }
    @PostMapping("/cancelOrder")
    @ResponseBody
    public Object cancelOrder(String orderId) {
        OrderDO order = orderService.queryById(orderId);
        if(null == order){
            return ResponseModel.Failure("参数错误");
        }
        if (orderService.updateOrderState(order, PayConstant.ORDER_STATE_CANCEL)) {
            return ResponseModel.Success("取消成功");
        }
        return ResponseModel.Failure("取消失败");
    }
    @PostMapping("/delOrder")
    @ResponseBody
    public Object delOrder(String orderId) {
        if (orderService.delOrder(orderId)) {
            return ResponseModel.Success("删除成功");
        }
        return ResponseModel.Failure("删除失败");
    }

    /**
     * 邮箱中的确认订单
     * @param orderId
     * @return
     */
    @GetMapping("/checkSuccess")
    public String success(String orderId,Integer state){
        OrderDO order = orderService.queryById(orderId);
        if(null == order){
            return "web/view/pay/pay-error";
        }
        if (orderService.updateOrderState(order,state)) {
            return "web/view/pay/pay-success";
        }

        return "web/view/pay/pay-error";
    }

}
